package projet.volleystat;

import java.util.ArrayList;

import projet.volleystat.bdd.VolleyOpenhelper;
import projet.volleystat.bdd.VolleyRepository;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class StatSelectTeamActivity extends Activity {
	protected ListView listv;
	protected SimpleCursorAdapter cadapter;
	protected Cursor query;
	protected VolleyRepository volleyDataBase;
	protected String order;
	protected ArrayList<Player> players;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_stat_select_team);

		volleyDataBase = new VolleyRepository(getApplicationContext());

		volleyDataBase.open();
		order = VolleyOpenhelper.Team_Columns_NAME;

		try {
			query = volleyDataBase.getAllCursorTeam(order);
		} catch (DatabaseException e) {
			e.printStackTrace();
		}

		listv = (ListView) findViewById(R.id.teamslv);
		cadapter = new SimpleCursorAdapter(getApplicationContext(),
				R.layout.listeitemlineteam, query, new String[] {
						VolleyOpenhelper.Team_Columns_NAME,
						VolleyOpenhelper.Team_Columns_COUNTRY }, new int[] {
						R.id.textteamname, R.id.textteamcountry });

		listv.setAdapter(cadapter);

		listv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				try {

					players = volleyDataBase.getAllPlayerInTeam(volleyDataBase
							.getTeamById((int) id));
					Intent intent = new Intent(getApplicationContext(),
							StatPlayerActivity.class);
					ArrayList<Integer> playersid = new ArrayList<Integer>();
					for (int i = 0; i < players.size(); i++) {
						playersid.add(players.get(i).getId());
					}
					intent.putExtra("selectedplayersarrayid", playersid);
					intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
					startActivity(intent);

				} catch (DatabaseException e) {

					players = new ArrayList<Player>();
					Toast toast = Toast.makeText(StatSelectTeamActivity.this,
							R.string.dberror, Toast.LENGTH_LONG);
					toast.show();
					e.printStackTrace();

				} catch (NullPointerException e) {

					players = new ArrayList<Player>();
					Toast toast = Toast.makeText(StatSelectTeamActivity.this,
							R.string.dberror, Toast.LENGTH_LONG);
					toast.show();
					e.printStackTrace();
				}
			}
		});
	}

	@Override
	public void onResume() {
		super.onResume();
		volleyDataBase.open();
		try {
			query = volleyDataBase.getAllCursorTeam(order);
		} catch (DatabaseException e) {
			e.printStackTrace();
		}
		cadapter.changeCursor(query);
	}

	@Override
	public void onPause() {
		super.onPause();
		query.close();
		volleyDataBase.close();
		overridePendingTransition(0, 0);
	}
}